package com.example.sjh_web.other

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.example.sjh_web.R
import kotlinx.android.synthetic.main.sjh_favorites_activity.*
import kotlinx.android.synthetic.main.sjh_favorites_item.view.*
import okhttp3.OkHttpClient
import okhttp3.Request
import kotlin.concurrent.thread

class sjh_favoritesAdapter (activity: Activity, val resourceId: Int, data: List<sjh_favoritesItem>): ArrayAdapter<sjh_favoritesItem>(activity, resourceId, data){
    var flag :Boolean = false

    fun  setflag(boolean: Boolean){
        this.flag = boolean
    }

    inner  class ViewHolder(val favorites_url : TextView, val favorites_title: TextView, var favorites_check: CheckBox)
    @SuppressLint("CutPasteId", "ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View
        val viewHolder: ViewHolder
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(resourceId, parent, false)
            val favorites_url : TextView = view.findViewById(R.id.favorites_url)
            val favorites_title : TextView = view.findViewById(R.id.favorites_title)
            val favorites_check :CheckBox = view.findViewById(R.id.favorites_checkbox)
            viewHolder = ViewHolder(favorites_url, favorites_title, favorites_check)
            view.tag = viewHolder
        }
        else{
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        val favorites = getItem(position)
        if(favorites != null){
            viewHolder.favorites_url.text = favorites.url
            viewHolder.favorites_title.text = favorites.title
            viewHolder.favorites_check.isChecked = favorites.check
        }

        if (flag){
            view.favorites_checkbox.visibility = View.VISIBLE
        }else{
            view.favorites_checkbox.visibility = View.GONE
        }

        return view
    }

}